package com.zmn.oms.dubbo.impl.quotation;

import com.alibaba.fastjson.JSON;
import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.oms.common.constant.OrderFailConsts;
import com.zmn.oms.common.constant.TriggerAmountChangeEnum;
import com.zmn.oms.common.dio.quotation3.*;
import com.zmn.oms.common.dro.quotation3.OperateResultDRO;
import com.zmn.oms.common.dto.OmsOperator;
import com.zmn.oms.model.dto.work.modify.amount.ZsDiscountDTO;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.quotation3.OrderQuotation3ModifyBService;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.dubbo.interfaces.quotation.Quotation3OrderWorkModifyRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author sunlife
 * @date:2021/12/1 6:41 下午
 * description:
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 20000, retries = 0, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class Quotation3OrderWorkModifyRemoteServiceImpl implements Quotation3OrderWorkModifyRemoteService {

    @Autowired
    OrderQuotation3ModifyBService quotation3ModifyBService;
    @Autowired
    OrderWorkService orderWorkService;

    @Override
    public ResponseDTO sendFaultConfirmMessage(OmsOperator operator) {
        log.info("推送故障模板消息 {}", JSON.toJSONString(operator));
        try {
            quotation3ModifyBService.sendFaultConfirmMessage(operator);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            log.error("推送故障模板消息报错", e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseDTO startCheck(OmsOperator operator) throws OmsBaseException {
        try {
            quotation3ModifyBService.startCheck(operator);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            log.error("保存开始检修报错", e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseDTO<OperateResultDRO> confirmCheckType(ConfirmCheckTypeDIO dio) throws OmsBaseException {
        try {
            log.info("工单[{}]确认检修方式 ：{}", dio.getWorkId(), JSON.toJSONString(dio));
            OperateResultDRO result = quotation3ModifyBService.confirmCheckType(dio);
            return ResponseDTO.success(result);
        } catch (OmsBaseException e) {
            log.error("确认检修方式错误", e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseDTO<OperateResultDRO> submitFaultCode(SubmitFaultCodeDIO dio) throws OmsBaseException {
        try {
            log.info("工单[{}]提交故障码 ：{}", dio.getWorkId(), JSON.toJSONString(dio));
            OperateResultDRO result = quotation3ModifyBService.submitFaultCode(dio);
            return ResponseDTO.success(result);
        } catch (Exception e) {
            log.error("提交故障码错误", e);
            return ResponseDTO.fail("提交故障码错误");
        }
    }

    @Override
    public ResponseDTO<OperateResultDRO> confirmFaultInfo(ConfirmFaultInfoDIO confirmFaultInfoDIO) {
        log.info("确认故障现象 {}", JSON.toJSONString(confirmFaultInfoDIO));
        try {
            OperateResultDRO result = quotation3ModifyBService.confirmFaultInfo(confirmFaultInfoDIO);
            return ResponseDTO.success(result);
        } catch (OmsBaseException e) {
            log.error("确认故障现象报错", e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseDTO submitCheckReport(@Valid CheckReportDIO checkReportDIO) {
        log.info("提交检测报告 {}", JSON.toJSONString(checkReportDIO));
        try {
            quotation3ModifyBService.submitCheckReport(checkReportDIO);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            log.error("提交检测报告报错", e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseDTO confirmCheckReportByEngineer(@Valid ConfirmCheckReportDIO confirmCheckReportDIO) {
        log.info("确认检修报告 {}", JSON.toJSONString(confirmCheckReportDIO));
        // 用户同意维修需要确认工单价格
        try {
            quotation3ModifyBService.confirmCheckReportByEngineer(confirmCheckReportDIO);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            log.error("确认检修报告报错,入参:{},错误原因:{}",JSON.toJSONString(confirmCheckReportDIO), e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseDTO submitConfirmRepairResultByEngineer(ConfirmRepairDIO confirmRepairDIO) {
        //log.info("工程师确认维修结果 {}", JSON.toJSONString(confirmRepairDIO));
        return ResponseDTO.success();
        // 用户同意维修需要确认工单价格
        /*try {
            //quotation3ModifyBService.submitConfirmRepairResultByEngineer(confirmRepairDIO);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            log.error("工程师确认维修结果报错", e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }*/
    }

    @Override
    public ResponseDTO sendCaptcha(SendAgreeCapthaDIO sendAgreeCapthaDIO) {
        log.info("发送同意维修验证码 {}", JSON.toJSONString(sendAgreeCapthaDIO));
        // 用户同意维修需要确认工单价格
        try {
            quotation3ModifyBService.sendCaptcha(sendAgreeCapthaDIO);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            log.error("发送同意维修验证码", e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseDTO submitConfirmRepairResult(ConfirmRepairDIO confirmRepairDIO) {
        log.info("确认维修结果 {}", JSON.toJSONString(confirmRepairDIO));
        // 用户同意维修需要确认工单价格
        try {
            quotation3ModifyBService.submitConfirmRepairResult(confirmRepairDIO);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            log.error("确认维修结果报错", e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseDTO submitConfirmRepairResultWithCaptcha(ConfirmRepairDIO confirmRepairDIO) {
        log.info("用户通过验证码操作同意维修 {}", JSON.toJSONString(confirmRepairDIO));
        // 用户同意维修需要确认工单价格
        try {
            quotation3ModifyBService.submitConfirmRepairResultWithCaptcha(confirmRepairDIO);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            log.error("确认维修结果报错", e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseDTO confirmUserAgreeResultByEngineer(ConfirmRepairDIO confirmRepairDIO) {
        log.info("确认维修结果：{}", JSON.toJSONString(confirmRepairDIO));
        // 用户同意维修需要确认工单价格
        try {
            quotation3ModifyBService.confirmUserAgreeResultByEngineer(confirmRepairDIO);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            log.error("确认维修结果报错", e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseDTO<String> addDiscountByEngineer(EngineerDiscountDIO engineerDiscountDIO) {
        log.info("添加工程师优惠 {}", JSON.toJSONString(engineerDiscountDIO));
        try {
            String url = quotation3ModifyBService.addDiscountByEngineer(engineerDiscountDIO);
            return ResponseDTO.success(url);
        } catch (OmsBaseException e) {
            log.error("添加工程师优惠报错", e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseDTO useEngineerDiscount(EngineerDiscountDIO engineerDiscountDIO) {
        log.info("使用工程师优惠 {}", JSON.toJSONString(engineerDiscountDIO));
        try {
            quotation3ModifyBService.useEngineerDiscount(engineerDiscountDIO);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            log.error("使用工程师优惠报错", e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseDTO saveDiscount(ArtificialDiscountDIO artificialDiscountDIO) {
        log.info("保存人工优惠 {}", JSON.toJSONString(artificialDiscountDIO));
        try {
            quotation3ModifyBService.saveDiscount(artificialDiscountDIO);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            log.error("保存人工优惠报错", e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseDTO sendDiscountToUser(MobileDIO mobileDIO) {
        log.info("向用户推送活动信息 {}", JSON.toJSONString(mobileDIO));
        try {
            quotation3ModifyBService.sendDiscountToUser(mobileDIO);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            log.error("向用户推送活动信息失败", e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseDTO addOrderAcceptMaintainItem(AddOrderAcceptMaintainDIO addOrderAcceptMaintainDIO) {
        log.info("[{}]添加订单工程师验收检测项目： 【{}】", addOrderAcceptMaintainDIO.getOrderId(), JSON.toJSONString(addOrderAcceptMaintainDIO));
        try {
            addOrderAcceptMaintainDIO.setOperateTime(DateUtil.getNow());
            quotation3ModifyBService.addOrderAcceptMaintainItem(addOrderAcceptMaintainDIO);
            return ResponseDTO.success();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("[{}]添加订单工程师验收检测项目：【{}】", addOrderAcceptMaintainDIO.getOrderId(), e);
            return ResponseDTO.fail();
        }
    }

    @Override
    public ResponseDTO clearEngineerDiscount(OmsOperator operator) {
        log.info("清空工程师优惠 orderId={},workId={}", operator.getOrderId(), operator.getWorkId());
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(operator.getOrderId(), operator.getWorkId());
        if (!Objects.equals(orderWork.getFinalPrice(), GlobalConsts.YES)) {
            return ResponseDTO.success();
        }
        try {
            quotation3ModifyBService.clearEngineerDiscount(orderWork, true);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            e.printStackTrace();
            log.error("[{}]清空工程师优惠失败：【{}】", operator.getOrderId(), e);
            return ResponseDTO.fail(e.getMessage());
        }
    }
}
